<include file="public@storage"/>
</head>
<body>
<div class="container body-content" style="margin-top:20px;width: 100%">
    <div class="panel panel-primary" style="margin-bottom: 0.5rem">
        <div class="panel-heading">厂商管理</div>
        <div class="panel-body">
            <div class="row">
                <div class="col-sm-12">
                    <div id="toolbar" class="btn-group">
                        <button id="addUser" class="btn btn-info" style="margin-left: 1rem; width: 10rem"
                                onclick="addSupplier()">添加厂商
                        </button>

                        <button  class="btn btn-info" style="margin-left: 1rem; width: 10rem"
                                onclick="updateSupplier()">更新库存
                        </button>
                    </div>
                    <table id="table"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    $(function () {
        init();
    });

    function init() {
        var url = "{:url('storage/Supplier/getSupplier')}";
        var columns = [
            {
                title: '序号',
                width: 50,
                align: "center",
                valign: 'middle',
                formatter: function (value, row, index) {
                    return index + 1;
                }
            },
            {
                title: 'uuid',
                field: 'uuid',
                visible: false
            },
            {
                title: '厂商编码',
                align: "center",
                field: 'supplier_id',
                filter: {
                    type: 'input'
                }
            },
            {
                title: '厂商名',
                align: "center",
                field: 'company_name',
                filter: {
                    type: 'input'
                }
            },
            {
                title: '厂商地址',
                align: "center",
                field: 'company_address',
                filter: {
                    type: 'input'
                }
            },
            {
                title: '操作',
                field: 'type',
                align: "center",
                valign: "middle",
                formatter: function (value, row, index) {
                    html = '<a onclick="editSupplier(this)" href="javascript:void(0);" data-type="' + row.type + '" data-uuid="' + row.uuid + '">编辑</a>';
                    html += '&ensp;|&ensp;';
                    html += '<a onclick="deleteSupplier(this)" href="javascript:void(0);" data-type="' + row.type + '" data-uuid="' + row.uuid + '" data-name ="'+row.company_name+'">删除</a>';
                    html += '&ensp;|&ensp;';
                    html += '<a onclick="updateInventory(this)" href="javascript:void(0);" data-type="' + row.type + '" data-id="' + row.supplier_id + '" data-name ="'+row.company_name+'">更新库存</a>';
                    return html;
                }
            }
        ];
        initTable(url, columns, 'server', InitSubTable);
    }

    InitSubTable = function (index, row, $detail) {
        var tableId = "cur_table_"+index;
        $detail.html('<table id="'+ tableId+ '"></table>').find('table');
        var supplierId = row.supplier_id;
        var url = '{:url("storage/Supplier/getSupplierInventory")}?supplierId=' + supplierId;
        var column = [
            {
                title: '序号',
                width: 50,
                align: "center",
                valign: 'middle',
                formatter: function (value, row, index) {
                    return index + 1;
                }
            },
            {
                title: 'uuid',
                field: 'uuid',
                visible: false
            },
            {
                title: '料号',
                field: 'sub_materiel_id',
                align: "center",
                filter: {
                    type: 'input'
                }
            },
            {
                title: '料名',
                field: 'sub_materiel_name',
                align: "center",
                filter: {
                    type: 'input'
                }
            },
            {
                title: '数量',
                field: 'count',
                align: "center",
                filter: {
                    type: 'input'
                }
            }
        ];
        initSubTable(tableId,url, column, 'server');
    };

    addSupplier = function(){
        window.open('/storage/Supplier/add', '_self');
    };

    editSupplier= function (that) {
        var type = $(that).data('type');
        var uuid = $(that).data('uuid');
        if (type == 0){
            layer.msg('委外厂商不可编辑');
        } else {
            window.open('/storage/Supplier/edit?uuid=' + uuid, '_self');
        }
    };

    deleteSupplier = function (that) {
        var uuid = $(that).data('uuid');
        var name = $(that).data('name');
        var type = $(that).data('type');
        if (type == 0) {
            layer.msg('委外厂商不可删除');
        } else {
            var data = {
                uuid: uuid
            };
            var tip = '是否删除厂商' + name;
            var url = "{:url('Supplier/deleteSupplier')}";
            reviseTable(tip, url, data);
        }
    };

    updateInventory = function (that) {
        var id = $(that).data('id');
        var name = $(that).data('name');
        var type = $(that).data('type');
        if (type == 1) {
            layer.msg('本地厂商不可更新库存');
        } else {
            var data = {
                supplierId: id
            };
            var tip = '是否更新厂商' + name+'库存';
            var url = "{:url('Supplier/updateInventoryFromK3')}";
            reviseTable(tip, url, data);
        }
    };

    updateSupplier = function () {
        var tip = '是否更新厂商库存';
        var url = "{:url('Supplier/updateInventoryFromK3')}";
        reviseTable(tip, url);
    }

</script>
</html>